//
//  HECireleListViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/5/10.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HECireleListViewController.h"
#import "HECircleCell.h"

#import "HECircleChatManager.h"
#import "HEIMCircleChatController.h"
#import "HEApplyCirlceSquareViewController.h"
#import "HEManageSpecView.h"

#import "HEPatientsCircleViewController.h"

@interface HECireleListViewController ()<UITableViewDelegate, UITableViewDataSource, HECircleChatManagerDelegate>

@property (nonatomic, strong) NSMutableArray <HEChatRoomModel *> *dataSource;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) BOOL hasNotice;

@end

static NSString *const JPSuspensionCacheMsgKey = @"JPSuspensionCacheMsgKey";
static NSString *const JPSuspensionDefaultXKey = @"JPSuspensionDefaultXKey";
static NSString *const JPSuspensionDefaultYKey = @"JPSuspensionDefaultYKey";

@implementation HECireleListViewController

- (NSMutableArray<HEChatRoomModel *> *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    JPSEInstance.navCtr = self.navigationController;
    JPSEInstance.willSpreadSuspensionViewControllerBlock = ^(UIViewController<JPSuspensionEntranceProtocol> *targetVC) {
        [(HEIMCircleChatController *)targetVC setIsHideNavBar: YES];
    };
    
    if (!_hasNotice) {
        [self popSpecViewClick];
        _hasNotice = YES;
    }
}

- (void)setupJPSEInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        // 是否可以播放提示音
        JPSEInstance.canPlaySound = YES;
        
//        // 配置展开时的提示音
//        JPSEInstance.playSpreadSoundBlock = ^{
//
//        };
//
//        // 配置闭合时的提示音
//        JPSEInstance.playShrinkSoundBlock = ^{
//
//        };
        
        JPSEInstance.cacheMsgBlock = ^(NSString *cacheMsg) {
            [[NSUserDefaults standardUserDefaults] setObject:cacheMsg forKey:JPSuspensionCacheMsgKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
        };
        
        JPSEInstance.cacheSuspensionFrameBlock = ^(CGRect suspensionFrame) {
            [[NSUserDefaults standardUserDefaults] setFloat:suspensionFrame.origin.x forKey:JPSuspensionDefaultXKey];
            [[NSUserDefaults standardUserDefaults] setFloat:suspensionFrame.origin.y forKey:JPSuspensionDefaultYKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
        };
        
    });
    
//    [self setupSuspensionView];
}

- (void)setupSuspensionView {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *cachaMsg = [[NSUserDefaults standardUserDefaults] stringForKey:JPSuspensionCacheMsgKey];
        if (cachaMsg) {
            HEPatientsCircleViewController *webVC = [[HEPatientsCircleViewController alloc] init];
            CGFloat x = [[NSUserDefaults standardUserDefaults] floatForKey:JPSuspensionDefaultXKey];
            CGFloat y = [[NSUserDefaults standardUserDefaults] floatForKey:JPSuspensionDefaultYKey];
            [JPSEInstance setupSuspensionViewWithTargetVC:webVC suspensionXY:CGPointMake(x, y)];
        }
    });
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KChangeFontNotification object:nil];
    [[HECircleChatManager sharedManager] removeDelegate:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
      self.title = @"圈子";
       
      [self initNav];
      [self initUI];
 
      [self setupJPSEInstance];
      
      [[HECircleChatManager sharedManager] addDelegate: self];
      [[HECircleChatManager sharedManager] initOpration];
      
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:KChangeFontNotification object:nil];
}

- (void)initUI {
  
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight - ZXSafeAreaBottom) style:UITableViewStyleGrouped];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = KHexColor(@"EEEEEE");

    _tableView.scrollsToTop = YES;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];

    self.tableView.backgroundColor = CViewBgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

 
    [_tableView registerClass:[HECircleCell class] forCellReuseIdentifier: @"HECircleCell"];
}


- (void)refresh {
    [self.tableView reloadData];
}

- (void)initNav {
    [self zx_setRightBtnWithText:@"申请" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        HEApplyCirlceSquareViewController *vc = [HEApplyCirlceSquareViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    self.zx_navRightBtn.zx_tintColor = CNavBgColor;
}
#pragma mark -
- (void)popSpecViewClick {
    HEManageSpecView *manageView = [HEManageSpecView new];
    manageView.vc = self;
    [manageView show];
}

#pragma mark - HECircleChatManagerDelegate
- (void)allTeams:(NSArray <HEChatRoomModel *> *)teams {
    _dataSource = [NSMutableArray arrayWithArray:teams];
    [self refresh];
}

- (void)updateTeamInfo:(HEChatRoomModel *)team index:(NSInteger)index {
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
//    HECircleTeamCell *cell =  [self.collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentitier forIndexPath:indexPath];
 
    [self.tableView reloadData];
}
  
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HECircleCell *cell = [tableView dequeueReusableCellWithIdentifier: @"HECircleCell"];
    [cell configCellWithModel: _dataSource[indexPath.row] indexPath:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:  @"HECircleCell" cacheByIndexPath:indexPath configuration:^(HECircleCell *cell) {
        [cell configCellWithModel: self.dataSource[indexPath.row] indexPath:indexPath];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEChatRoomModel *model = self.dataSource[indexPath.row];
    
//    JPSEInstance.suspensionView.hidden = YES;

    [[HECircleChatManager sharedManager] switchRoomVC:model isFromChatPage:NO callback:^(HEIMCircleChatController * _Nonnull vc) {
            
        }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}


@end
